<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="实例名称" prop="instanceName">
        <el-input
          v-model="queryParams.instanceName"
          placeholder="请输入实例名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="执行状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择执行状态" clearable>
          <el-option
            v-for="dict in dict.type.data_sync_instance_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['data:instance:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="instanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="实例id" align="center" prop="instanceId" /> -->
      <el-table-column label="实例名称" align="center" prop="instanceName" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.data_sync_instance_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['data:instance:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-video-play"
            @click="handleDelete(scope.row)"
            v-hasPermi="['data:instance:remove']"
          >重复执行</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据同步实例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form label-width="80px">
        <el-form-item label="实例名称">
          <span>{{ form.instanceName }}</span>
        </el-form-item>
        <el-form-item label="任务id">
          <span>{{ form.jobId }}</span>
        </el-form-item>
        <el-form-item label="开始时间">
          <span>{{ form.startTime }}</span>
        </el-form-item>
        <el-form-item label="结束时间">
          <span>{{ form.endTime }}</span>
        </el-form-item>
        <el-form-item label="执行状态" prop="status">
          <el-radio-group v-model="form.status" :disabled="true">
            <el-radio
              v-for="dict in dict.type.data_sync_instance_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="任务耗时">
          <span>{{ form.timeConsing }}秒</span>
        </el-form-item>
        <el-form-item label="数据数量">
          <span>{{ form.dataTotal }}条</span>
        </el-form-item>
        <el-form-item label="成功数">
          <span>{{ form.dataSuccess }}条</span>
        </el-form-item>
        <el-form-item label="失败数">
          <span>{{ form.dataFail }}条</span>
        </el-form-item>
        <el-form-item label="失败原因">
          <span>{{ form.failReason }}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listInstance, getInstance, reSyncInstance} from "@/api/data/instance";

export default {
  name: "Instance",
  dicts: ['data_sync_instance_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据同步实例表格数据
      instanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        instanceName: null,
        startTime: null,
        endTime: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据同步实例列表 */
    getList() {
      this.loading = true;
      listInstance(this.queryParams).then(response => {
        this.instanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        instanceId: null,
        instanceName: null,
        jobId: null,
        startTime: null,
        endTime: null,
        status: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.instanceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查询详情按钮操作 */
    handleUpdate(row) {
      this.reset();
      const instanceId = row.instanceId || this.ids
      getInstance(instanceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "数据同步实例详情";
      });
    },
    /** 重复执行按钮操作 */
    handleDelete(row) {
      const instanceIds = row.instanceId || this.ids;
      this.$modal.confirm('是否重复执行').then(function() {
        return reSyncInstance(instanceIds);
      }).then((response) => {
        if(response.code == 200){
          this.$modal.msgSuccess(response.msg);
        }else{
          this.$modal.msgError(response.msg);
        }
        this.getList();
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('data/instance/export', {
        ...this.queryParams
      }, `instance_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
